package com.tensquare.qa.controller;

import com.tensquare.qa.client.LabelClient;
import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.ProblemService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ProblemService problemService;

    @Autowired
    private LabelClient labelClient;

    @RequestMapping("/label/{labelid}")
    public Result findLabelById(@PathVariable String labelid) {
        return labelClient.findById(labelid);
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Problem problem) {
        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null) {
            return new Result(StatusCode.ACCESSERROR, false, "无权访问");
        }
        problem.setUserId(claims.getId());
        problemService.add(problem);
        return new Result(StatusCode.OK, true, "添加成功");
    }

    //todo 回答问题

    //todo 发吐槽

    //todo 发文章
}
